<?php
class Goods{
    protected $main_table = 'data_goods';
	protected $main_table2 = 'data_goods_cate';
	protected $main_table3 = 'data_goods_comment';
	protected $main_id = 'goods_id';
	protected $main_id2 = 'cate_id';
	protected $main_id3 = 'comment_id';
	protected $db = null;
	
	public function __construct($db){
		$this->db=$db;
	}
   
	public function __destruct(){
		unset($this->db);
	}
	public function get_table_name(){
	    return $this->main_table;
	    
	}
	public function get_table_name2(){
	    return $this->main_table2;
	    
	}	
	public function get_table_name3(){
	    return $this->main_table3;
	    
	}	
	/**
	* 	insert goods
	* 	@param array $data 
	* 	@return int affected count
	*/
	public function insert_data($data){
		if(!is_array($data))
		my_jump('输入的参数有误');
		$rows = $this->db->insert($this->main_table, $data);
		$insert_id = $this->db->getInsertId();
		return $insert_id;	
	}
	
	public function insert_custom_data($data){
		if(!is_array($data))
		my_jump('输入的参数有误');
		$rows = $this->db->insert('data_custom', $data);
		$insert_id = $this->db->getInsertId();
		return $insert_id;	
	}
	
	public function insert_user_data($data){
		if(!is_array($data))
		my_jump('输入的参数有误');
		$rows = $this->db->insert($this->main_table2, $data);
		$insert_id = $this->db->getInsertId();
		return $insert_id;	
	}
	
	/**
	* 	update goods
	* 	@param array $data 
	* 	@param string $where 
	* 	@return int affected count
	*/
	public function update_data($data,$where){
		if(!is_array($data))
		my_jump('输入的参数有误');
		$result = $this->db->update($this->main_table,$data,$where);
		return $result;
	}
	
	/**
	* 	delete goods
	* 	@param string $where 
	* 	@return int affected count
	*/
	public function del_data($id){//删除图片	
		$row = $this->get_data_row($id,'goods_index_image,goods_images');
		$this->del_photo($row['goods_index_image']);
		$this->del_photo($row['goods_images']);
		$where = "goods_id='".$id."'";
		$result = $this->db->delete($this->main_table,$where);
		return $result;
	}	
	
	/**
	* 	get goods info
	* 	@param int $id 
	*	@param array/string $field
	* 	@return array one row data
	*/
	public function get_data_row($id,$field='*'){
		if(!intval($id))
		my_jump('输入的参数有误!');
		$sql = "SELECT ".$field." FROM ".$this->main_table." WHERE ".$this->main_id."='".intval($id)."' ";
		$data = $this->db->getRow($sql);
		return $data;
	}	
	
	/**
	* 	get goods array
	* 	@param string $where
	* 	@return array data
	*/
	public function get_data_array($where,$order='goods_id DESC',$limit='10',$field='goods_id,goods_name,goods_photo'){
		$sql = "SELECT ".$field." FROM ".$this->main_table." WHERE ".$where." ORDER BY ".$order;
		if(intval($limit)){
			$sql .= " LIMIT ".$limit;
		}
		$data = $this->db->getAll($sql);
		return $data;
	}   
	
	
	public function get_goods_for_cart($where='',$field='goods_id,goods_title,goods_index_photo,goods_original_price'){
		$sql = "SELECT ".$field." FROM ".$this->main_table." where goods_status='1'" ;
		
		if(trim($where)){
			$sql .= " and ".$where;
		}
		$data = $this->db->getAll($sql);
		
		if($data){
			$new =array();
			foreach($data as $v){
				$new[$v['goods_id']] = $v;
			}
			
		}
		return $new;
	}
	
	public function insert_data_cate($data){
		if(!is_array($data))
		my_jump('输入的参数有误');
		$rows = $this->db->insert($this->main_table2, $data);
		$insert_id = $this->db->getInsertId();
		$this->update_data_file();
		return $insert_id;	
	}
	
	public function get_cate_row($id,$field='*'){
		if(!intval($id))
		my_jump('输入的参数有误!');
		$sql = "SELECT ".$field." FROM ".$this->main_table2." WHERE ".$this->main_id2."='".intval($id)."' ";
		$data = $this->db->getRow($sql);
		return $data;
	}		


	public function get_cate_array($where,$order='cate_id DESC',$limit='10',$field='*'){
		$sql = "SELECT ".$field." FROM ".$this->main_table2." WHERE ".$where." ORDER BY ".$order;
		if(intval($limit)){
			$sql .= " LIMIT ".$limit;
		}
		$data = $this->db->getAll($sql);
		return $data;
	} 

	public function update_data_cate($data,$where){
		if(!is_array($data))
		my_jump('输入的参数有误');
		$result = $this->db->update($this->main_table2,$data,$where);
		$this->update_data_file();
		return $result;
	}

	
	public function del_data_cate($id){
		$result = $this->db->delete($this->main_table2,$this->main_id2."= '".$id."'");
		$this->update_data_file();
		return $result;
	}
	
	public function update_file(){
		$data = $this->get_cate_array("1",'cate_id DESC','','cate_id,cate_name');		
		if($data){
			$temp_ary = array();
			foreach($data as $k=>$v){
				$temp_ary[$v['cate_id']] = $v['cate_name'];
			}			
			$new_array = '$cf_goods_cate=';
			$new_array .= var_export($temp_ary,true);
			write_file(INFO_PATH.'cf_goods_cate.php',$new_array,"php");		
		}
		
	} 
	
	public function update_data_file(){
		$sql  = "select cate_id as id,cate_name as title,cate_pid as pid,cate_image_path as image,cate_sort_num as sort from data_goods_cate where 1 order by cate_sort_num";	
		$data = $this->db->getAll($sql);
		if($data){
			$newdata = loop_data(0,$data,'pid','id');
			$new_array = var_export($newdata,true);
			$input_data= "\$cf_var_cate=".$new_array;
			write_file(INFO_PATH."cf_var_cate.php",$input_data);
			
			$newdata2 = loop_data(0,$data,'pid','id',0);
			$json = json_encode($newdata2);
			write_file(INFO_PATH."cf_var_cate.js",$json,"js");
			
		}
	}
	
	public function load_cate_data($file='cf_var_cate.php'){		
		$file = INFO_PATH.$file;
		if(is_file($file)){
			require($file);			
			return $cf_var_cate;
		}else{
			die("文件加载失败");
		}
	}
	
	public function get_comment_row($id,$field='*'){
		if(!intval($id))
		my_jump('输入的参数有误!');
		$sql = "SELECT ".$field." FROM ".$this->main_table3." WHERE ".$this->main_id3."='".intval($id)."' ";
		$data = $this->db->getRow($sql);
		return $data;
	}
	
	public function get_comment_array($where,$order='comment_id DESC',$limit='10',$field='*'){
		$sql = "SELECT ".$field." FROM ".$this->main_table3." WHERE ".$where." ORDER BY ".$order;
		if(intval($limit)){
			$sql .= " LIMIT ".$limit;
		}
		$data = $this->db->getAll($sql);
		return $data;
	}   
	
	/**
	* 	insert goods
	* 	@param array $data 
	* 	@return int affected count
	*/
	public function insert_data_comment($data){
		if(!is_array($data))
		my_jump('输入的参数有误');
		$rows = $this->db->insert($this->main_table3, $data);
		$insert_id = $this->db->getInsertId();
		return $insert_id;	
	}
	
	public function update_comment($data,$where){
		if(!is_array($data))
		my_jump('输入的参数有误');
		$result = $this->db->update($this->main_table3,$data,$where);
		return $result;
	}
	
	//为你推荐的数据
	public function goods_recommend($limit=3){
		$sql = "SELECT * FROM data_goods ORDER BY RAND() LIMIT ".$limit;
		$list = $this->db->getAll($sql);		
		return $list;	
	}
	
	/**
	* 	delete photos
	* 	@param string $photos 
	*/
	public function del_photo($photos){
		if($photos){
			$array = explode(',',$photos);
			foreach($array as $photo){
				$file = trim($photo,'/');
				$file2 = trim(get_thumb_name($photo),'/');	
				
				unlink($file);				
				unlink($file2);
			}
		}		
	}
	
	//添加发起者
	public function add_first_user($gid,$uid,$user){		
		$first_user = $this->get_cate_row($gid,'goods_board_first_user');
		if($first_user){
			return true;	
		}else{
			$data = array('goods_board_first_uid'=>$uid,'goods_board_first_user'=>$user);
			$where = "goods_id = '".$gid."'";
			$this->update_data($data,$where);
		}	
			
	}	

	public function get_goods_list($where,$page,$order="goods_id DESC",$field="*",$pageCount=10){
		$sql = "SELECT ".$field." FROM ".$this->main_table." WHERE ".$where." ORDER BY ".$order." LIMIT ".($page-1)*$pageCount.",".$pageCount;
		$list = $this->db->getAll($sql);		
		return $list;
	}

	//获取微课堂回顾的数据 根据同一个讲师查询微课堂的数据
	public function get_classroom_history($id){
		$field = 'goods_id,goods_name,goods_price,goods_index_image';
		$where = "goods_classroom_teacher_id = '".$id."' AND goods_classroom_time < '".date('Y-m-d')."'";
		$sql = "SELECT ".$field." FROM ".$this->main_table." WHERE ".$where." ORDER BY goods_id DESC ";		
		$list = $this->db->getAll($sql);		
		return $list;
	}

	//获取推荐微课堂的数据
	public function get_recommend_classroom(){
		$field = 'goods_id,goods_name,goods_price,goods_index_image,goods_classroom_time';
		$where = "goods_is_index = '1' AND goods_is_classroom = '1' AND goods_classroom_time > '".date('Y-m-d')."'";
		$sql = "SELECT ".$field." FROM ".$this->main_table." WHERE ".$where." ORDER BY goods_id DESC";
		$list = $this->db->getAll($sql);
		if($list){
			load_file('log_class',LIB_PATH);
	    	$log = new Log($this->db);
	    	$goods_id = '';
	    	//查询购买用户的数据
	    	foreach($temp_data as $v){
	    		$goods_id .= $v['goods_id'].',';
			}
			$goods_id = trim($goods_id,',');
			$where = "gbl_gid IN (".$goods_id.",808)";
			$temp_log_list = $log->get_goods_buy_log($where);
			if($temp_log_list){
				foreach($temp_log_list as $v){//转化为以商品ID的键值的数组					
					$log_list[$v['gbl_gid']][] = $v;	    			
				}
			}

			foreach($list as $k=>$v){				
				$gbl_row = $log_list[$v['goods_id']];
				if($gbl_row){					
					$list[$k]['log_list_count'] = count($gbl_row);
					$list[$k]['log_list'] = array_slice($gbl_row, 0, 5);					
				}								
				$list[$k]['goods_classroom_time'] = date("m月d日",strtotime($v['goods_classroom_time']));
				
				//$data[$v['goods_classroom_city']]['list'][] = $v;
			}



		}		
		return $list;
	}

	
}

?>